<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of dBModule
 *
 * @author dionisrom
 */
class dBModule extends LoadConfigs 
{
	public function __construct() {
		parent::__construct();
	}
	
	public function selectOneRow($fetchString = "", array $params = array()) {
		$limitOneRow = " LIMIT 1";
		
		foreach ($params as $key => $value) {
			if ($key == "table") {
				$fetchString = str_replace("::$key::", $this->_options["dbConfig"]["prefix"] . $value, $fetchString);
			} else {
				$fetchString = str_replace("::$key::", $value, $fetchString);
			}
		}
		$fetchString = $fetchString . $limitOneRow;

		return $this->execute($fetchString);
	}
	
	public function selectAll($fetchString = "", array $params = array()) {
		foreach ($params as $key => $value) {
			if ($key == "table") {
				str_replace("::$key::", $this->_options["dbConfig"]["prefix"] . $value, $fetchString);
			} else {
				str_replace("::$key::", $value, $fetchString);
			}
		}

		return $this->execute($fetchString);
	}
	
	public function execute($sql = '') {
		if (empty($sql)) {
			return null;
		}
			
		$query = mysql_query($sql, $this->_options["dbConn"]) or die(mysql_errno());
		//TODO get result in array
		if (!is_array($result) || (is_array($result) && count($result) == 0)) {
			return null;
		}
		
		$result = array();
		while ($row = mysql_fetch_array($query, FETCH_MODE)) {
			$key = key($row);
			$result[$key] = $row[$key];
		}
		
		return $result;
	}
}

?>
